﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;
using Dapper;
using Common;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class ValueControlDAL
    {
        public static Task<IEnumerable<ValveControl>> QueryControlValues(ValveControlQueryCriteria criteria = null)
        {
            ValveControlQueryCriteria queryCriteria = criteria ?? new ValveControlQueryCriteria();

            string sql = @"select t1.Id
                        ,t1.operator
                        ,t1.addtime
                        ,t1.exec_time
                        ,t1.msg_id
                        ,t1.valve_no
                        ,t1.orderType
                        ,t1.`order`
                        ,t1.time1
                        ,t1.time2
                        ,t1.time3
                        ,t1.STATUS
                        ,t1.update_time
                        ,t1.hasOper
                        ,t2.stationId
                        ,t2.stationName
                        ,t2.devinceName AS DeviceName
                        ,t2.valve_noList as Valve_NoListStr
                        from msap_db.valve_ctrl t1 
                        left join msap_db.station_infor t2 
                        on t1.station_id = t2.stationId  
                        where 
                        (@UserId IS NULL OR t1.operator = @UserId)
                        AND(@StationId IS NULL OR t2.stationId = @StationId)
                        AND(@Status IS NULL OR t1.Status = @Status)"
                        ;
            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return conn.QueryAsync<ValveControl,StationInfo,ValveControl>(sql,(controlValue, stationInfo) => {
                    controlValue.StationInfo = stationInfo ?? new StationInfo();
                    return controlValue;
                 }, 
                 new
                 {
                     UserId = queryCriteria.UserId,
                     StationId = queryCriteria.StationId,
                     Status = queryCriteria.Status
                 }, splitOn: "stationId");
            }
        }

        public static void InsertControlValues(List<ValveControl> valueControlList)
        {           
            string sql = @"INSERT INTO msap_db.valve_ctrl
                            (
                                id
                                , station_id
                                ,operator
                                , addtime
                                , exec_time
                                , msg_id
                                , valve_no
                                , orderType
                                ,`order`
                                , time1
                                , time2
                                , time3
                                ,`status`
                                , update_time
                                , hasoper
                            )
                            value
                            (
                                @Id
                                , @Station_Id
                                , @Operator
                                , @AddTime
                                , @ExecTime
                                , @MsgId
                                , @ValveNo
                                , @OrderType
                                , @Order
                                , @Time1
                                , @Time2
                                , @Time3
                                , @Status
                                , @UpdateTime
                                , @HasOper
                            )";

            foreach (ValveControl valveControl in valueControlList)
            {
                using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
                {
                    conn.ExecuteAsync(sql, new
                    {
                        Id = Guid.NewGuid().ToString(),
                        Station_Id = valveControl.StationInfo.StationId,
                        Operator = valveControl.Operator,
                        AddTime = valveControl.AddTime,
                        ExecTime = valveControl.Exec_Time,
                        MsgId = valveControl.Msg_Id,
                        ValveNo = valveControl.Valve_No,
                        OrderType = valveControl.OrderType,
                        Order = valveControl.Order,
                        Time1 = valveControl.Time1,
                        Time2 = valveControl.Time2,
                        Time3 = valveControl.Time3,
                        Status = valveControl.Status,
                        UpdateTime = valveControl.Update_Time,
                        HasOper = valveControl.HasOper
                    });
                }
            }
        }
    }
}
